﻿@page "/post/{PostName}"
@using System.IO;
@using Blog.Model;
@inject HttpClient HttpClient;

<div class="post-content well" style="word-break: break-all;">
    <article class="content">
        <div class="post">
            @HtmlContent
        </div>
    </article>
</div>


@code
{
    // 博客界面

    [Parameter]
    public string PostName { set; get; }

    [Parameter]
    public MarkupString HtmlContent { get; set; }

    private BlogModel Blog { set; get; }

    protected override async Task OnInitializedAsync()
    {
        var url = $"/Blog/{PostName}";
        Console.WriteLine(url);
        var text = await HttpClient.GetStringAsync(url);

        text = ParseBlog(text);

        var html = Markdig.Markdown.ToHtml(text);

        HtmlContent = new MarkupString(html);
    }

    private string ParseBlog(string str)
    {
        var stringReader = new StringReader(str);
        var title = stringReader.ReadLine() ?? "";

        if (title.StartsWith("#"))
        {
            title = title.Substring(1);
            title = title.Trim();
        }

        Blog = new BlogModel()
        {
            Title = title
        };

        return stringReader.ReadToEnd();
    }
}
